/*
============================================================================
 Name        : dd_bheap.h
 Author      : Jian (raphyer@hotmail.com)
 Version     :
 Copyright   : GPL
 Description : dd_bheap.h - header file
============================================================================
*/

#ifndef DD_BHEAP_H
#define DD_BHEAP_H

#include "dd_define.h"

__DD_BEGIN_DECLS

/* 
* The binary-heap structure Implement 
*/
typedef struct __dd_bheap dd_bheap ;
typedef void dd_bheap_value ;

struct __dd_vector ;

typedef enum {
    EDDMaxHeap = 0,
    EDDMinHeap
} dd_bheap_type;

struct __dd_bheap {
    
    dd_bheap_type        type ;
    struct __dd_vector*  vec ;

    int (*compare)(const dd_bheap_value* v1, const dd_bheap_value* v2) ;	/* 0: equal ; -1: v1 < v2 ; 1: v1 > v2 */
    void (*update_index)(dd_bheap_value* value, int index) ;     
    int (*index)(const dd_bheap_value* value) ;
};


dd_bheap* dd_bheap_create(
    dd_bheap_type type,
    int (*compare)(const dd_bheap_value*, const dd_bheap_value*), 
    void (*update_index)(dd_bheap_value* , int),     /* If you don't use the dd_bheap_change function, it cant be NULL */
    int (*index)(const dd_bheap_value*)              /* If you don't use the dd_bheap_change function, it cant be NULL */
) ;

dd_bheap* dd_bheap_create_by(
    dd_bheap_type type,
    struct __dd_vector* vec,
    int (*compare)(const dd_bheap_value*, const dd_bheap_value*), 
    void (*update_index)(dd_bheap_value* , int),     /* If you don't use the dd_bheap_change function, it cant be NULL */
    int (*index)(const dd_bheap_value*)              /* If you don't use the dd_bheap_change function, it cant be NULL */
) ;

void dd_bheap_destroy(dd_bheap* heap) ;

int dd_bheap_count(const dd_bheap* heap) ;

void dd_bheap_insert(dd_bheap* heap, dd_bheap_value* value) ;

dd_bheap_value* dd_bheap_delete(dd_bheap* heap) ;

void dd_bheap_change(dd_bheap* heap, dd_bheap_value* value) ;

void dd_bheap_sort(
    dd_bheap_type type, 
    struct __dd_vector* vec,
    int (*compare)(const dd_bheap_value*, const dd_bheap_value*)
) ;


#ifdef __DD_TEST

DD_BOOL dd_bheap_test() ;

#endif

__DD_END_DECLS

#endif	/* DD_BHEAP_H */

/* End of File */
